require(['zTreeCheckbox','select2', 'validator'], function () {
	var getTreeDataUrl = "/sysCorpInfo/getSysCorpInfoList.htm",	//左侧树url
		corpId,$sysForm=$("#sysForm"),
		$sysCorpTree = $("#sysCorpTree");//公司树对应页面元素

	//获得节点数据，并且生成左侧的树
	function initsysCorpTree(nodeId){
		var node;
		$.ajaxExt({
			url : getTreeDataUrl
		}).done(function(data){
			if (!data) {
				data = [];
			}
			// 树的参数配置
			var setting = {
				data: {
					key: {
						name: "corpName"
					},
					simpleData: {
						enable: true,
						idKey: "corpId",
						pIdKey: "parentCorpId",
						rootPId: 0
					}
				},
				view: {
					showIcon: true
				},
				callback: {
					onClick: sysCorpTreeClick
				}
			};
			//生成树
			sysCorpTreeObj = $.fn.zTree.init($sysCorpTree, setting, data);
			sysCorpTreeObj.expandAll(true);
			if(typeof nodeId !== "undefined"){//选中对应节点
				sysCorpTreeObj.setting.view.expandSpeed = "";
				sysCorpTreeObj.setting.view.expandSpeed = "fast";
				node = sysCorpTreeObj.getNodesByFilter(function(node) { return node.corpId == nodeId; }, true);
			}else{//选中第一个节点
				node = sysCorpTreeObj.getNodesByFilter(function(node) { return node.level == 0; }, true);
			}
			sysCorpTreeObj.selectNode(node,false);
			sysCorpTreeClick(null,node.corpId,node,true);
		});
	}
	
	function init() {
		$("#sysCorpForm").initForm();
		$(window).on('resize', initHeight).trigger('resize');
	    $('.slide-panel').on('click', function () {
	        $('#left').css('margin-left', $(this).toggleClass('slide-panel-active').hasClass('slide-panel-active') ? -250 : 0);
	    });
	}
	
	/**
	 * 进行初始化工作
	 */
	function initHeight(){
		var height = $(window).height();
		$sysCorpTree.parent().height(height - 45);
		$sysCorpTree.height(height - 89);
		$('#right').height(height);
	}
	
	/**
	 * 点击树节点
	 */
	function sysCorpTreeClick(event, treeId, treeNode,clickFlag){
		if(treeNode.corpId!=""){
			corpId = treeNode.corpId;
			//根据corpId查询企业评分设置
			getSysCompreAbilitySet(corpId);
			if(corpId=="39708c61759b48748c5bd853ba64ded5"){
				$("#modif").hide();
				$("#backSys").hide();
			}else{
				$("#modif").show();
				$("#backSys").show();
			}
		}else{
			$.messager.alert("提示","请选择一辆车进行查看。");
		}
	}

	// 新增、更新 记录
	$('#modif').on("click",function(){
		if($.validator($sysForm)){
			var data={};
			data =$sysForm.getValues();
			data.corpId = corpId;
			$.ajaxExt({
				url :"/sysCompreAbilitySet/modoifSysCompreAbilitySet.htm",
				data : data
			}).done(function(data){
				$.messager.alert("提示","保存成功");
			});
		}
	});
	
	//重置
	$('#backSys').on("click",function(){
		getSysCompreAbilitySet("39708C61759B48748C5BD853BA64DED5");
	});
	
	
	//根据corpId查询企业评分设置
	function getSysCompreAbilitySet(corpId){
		$.ajaxExt({
			url :"/sysCompreAbilitySet/getSysCompreAbilitySet.htm",
			data : {'corpId' : corpId}
		}).done(function(data){
			$("#mftCoef").val(data[0].mftCoef);
			$("#eneCoef").val(data[0].eneCoef);
			$("#bhvCoef").val(data[0].bhvCoef);
			$("#mainCoef").val(data[0].mainCoef);
		});
	}

	initsysCorpTree();

    init();
});